package com.trump.baiduim.controller;
import com.trump.baiduim.model.User;
import com.trump.baiduim.service.FriendshipService;
import com.trump.baiduim.utils.ErrorCode;
import com.trump.baiduim.utils.ResultUtil;
import com.trump.baiduim.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author xujb
 * @date 2020/11/24 17:56
 */
@RestController
@Slf4j
@RequestMapping("/api/friendship")
public class FriendshipController {

    @Autowired
    private FriendshipService friendshipService;

    @Autowired
    private ResultUtil resultUtil;

    @Autowired
    TokenUtil tokenUtil;

    @PostMapping("/deleteFriend")
    public ResultUtil deleteFriend(@RequestBody Map<String,Long> object){

        try {
            friendshipService.deleteFriend(object.get("friendshipId"));
            return resultUtil.ofSuccess();
        } catch (Exception e) {
            log.error("调用接口失败",e);
            return null;
        }
    }

    @PostMapping("/addFriend")
    public ResultUtil addFriend(@RequestBody Map<String,String> object){

        try {
            friendshipService.addFriend(object.get("friendshipSender"),object.get("friendshipReceiver"));

            return resultUtil.ofSuccess();
        } catch (Exception e) {
            log.error("调用接口失败",e);
            return null;
        }
    }
    /**
     * *******************
     */
    /**
     * 查询用户的通讯录（好友列表）
     *
     * @param map
     * @return
     */
    @PostMapping("/query")
    public ResultUtil findFriendList(@RequestBody Map<String,String> map){

        //1。判断用户账号字段是否存在，用户账号值是否为空
        if(!map.containsKey("userAccount")){
            return resultUtil.ofFail(ErrorCode.EXCEPTION.getCode(),"用户账号字段不存在！！！");
        }
        //获取用户账号userAccount
        String userAccount = map.get("userAccount");
        if(userAccount == null || "".equals(userAccount)) {
            return resultUtil.ofFail(ErrorCode.EXCEPTION.getCode(),"用户账号值不能为空！！！");
        }

        //2.判断token字段是否存在
        if(!map.containsKey("token")) {
            return resultUtil.ofFail(ErrorCode.EXCEPTION.getCode(),"token字段不存在！！！");
        }
        //获取token值
        String token = map.get("token");

        //3。验证token
        if(!tokenUtil.verify(token)) {
            return resultUtil.ofFail(ErrorCode.EXCEPTION.getCode(),"token验证失败！！！");
        }


        //4.通过用户账号获取用户的好友列表,按照好友昵称排序
        List<User> users = friendshipService.findFriendList(userAccount);
        return resultUtil.ofSuccess(users);
    }

    @RequestMapping(value = "/fuzzySearch", method = RequestMethod.POST)
    public ResultUtil fuzzySearch(@RequestBody Map<String,String> body) {
        String departmentName = "";
        String userName = "";
        try {
            if (body.containsKey("departmentName")) {
                departmentName = body.get("departmentName");
            }
            if (body.containsKey("userName")) {
                userName = body.get("userName");
            }

            List<User> data = friendshipService.fuzzySearch(departmentName,userName);
            return resultUtil.ofSuccess(data);
        } catch (Exception e) {
            log.error("调用模糊查找fuzzySearch接口失败",e);
            return resultUtil.ofFail(500,"调用模糊查询接口失败");
        }
    }
}
